Programming II - Functions and Iteration

GEOG 30323

January 28, 2020

Python refresher

Define…

  • Variables
  • Strings
  • Lists
  • Indexing and slicing

Functions

  • If you find yourself doing the same thing frequently, it can be tedious to write the same code over and over!
  • As such, you should use functions to make your code re-usable
  • Functions are defined with a def statement followed by a colon (:)
  • They include a series of parameters to which you supply arguments, which are like variables that operate within your function:

Empty functions

  • At a basic level, a function can be defined without parameters

Example:

Functions and parameters

Parameters are components of your function that can vary based on user input. Example:

The return statement

  • The output of functions can be assigned to variables if a return statement is provided

Example:

Python and whitespace

  • Python code is organized by indentation and whitespace
  • After function definitions, code should be indented with four spaces. In the Jupyter Notebook (and other Python development environments), the Tab key represents four spaces, and it will indent your code automatically
  • Code that is not indented properly will cause an error

Comments

  • Comments, preceded by a hash (#), can be included in your code but are not run
  • Commenting your code is useful for describing what you are doing, or keeping experimental/old code you don’t want to run during the development process

Docstrings

  • Docstrings can be used to describe what functions do
  • Docstrings are enclosed in triple double quotes (""" """) and are placed on the line following the function definition

Ordering and named arguments

Arguments can be supplied to functions in two ways:

  • “Unnamed” in the order specified
  • “Named” in any order. Be careful, however, if you mix the two!

Iteration

Iteration

  • At your jobs, you will often need to repeat the same task over and over!
  • From your textbook: “Repeating identical or similar tasks without making errors is something that computers do well and people do poorly.”
  • Solution: make the computer do it with iteration

Loops

  • Loops are operators that tell the computer to repeat an action a given number of times
  • Common loops:
    • for: Repeats an action over a series of items
    • while: Repeats an action until a given condition is satisfied
  • Important note: loops, like functions, must obey whitespace rules!

The for loop

  • for repeats an action for every element in an object
  • Example:

How for works

So what is going on here?

  1. The for loop looks at what it will iterate through - in this case it is a string, Kyle
  2. We are referring to each element of our string, Kyle, as character.
  3. The loop evaluates the expression passed to the print function for each character in Kyle successively

So the for loop is equivalent to:

The while loop

  • while repeats an action until a given condition is satisfied
  • Example:

How while works

  1. We define a counter, i, that we set to 5 before running the loop
  2. While the value of i is greater than 0, we tell Python to evaluate the print function
  3. With each run of the loop, we subtract 1 from i
  4. When i is equal to 0, we exit the loop and print "Blast-off" to the console

Beware of the infinite while loop!

Conditional logic

  • However - what if we don’t want to do the same thing every time we run a loop? Or a function, for that matter?
  • Answer: conditional logic

Conditional logic

  • Conditional statements in Python: if, elif, and else
  • Conditional operators:
    • < Less than
    • > Greater than
    • <= Less than or equal to
    • >= Greater than or equal to
    • == Is equal to
    • != Is not equal to
  • Booleans: True and False
  • Boolean operators: and, or, & not

Conditional logic in Python

  • Example:

Conditional logic in functions

  • When writing functions, you’ll rely heavily on conditional statements